/**
 * JDBC各种操作
 * @author 董一华
 * @since 1.00
 * @version 1.00 
 * 
 * 修订列表： v1.00 2009-10-24 
 * 			修订人：董一华 
 * 			修订内容：初始版本 
 */

package com.zjht.gmcc.bean.util;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

public class MyJDBC {

	private Connection conn = null;
	private ResultSet rs = null;
	private PreparedStatement ps = null;
	private CallableStatement cstmt = null;

	private boolean bClosed = false;
	private boolean bAutoCommit = true;

	/**
	 * 使用连接池获得Connection
	 * @author dyh 2009-10-24
	 */
	public Connection getConnection() {
		try {
			if (!JDBCUtil.isConnected(conn)) {
				DataSource ds = JDBCUtil.getDataSource();
				if (ds != null) {
					conn = ds.getConnection();
					bClosed = false;
				}
			}
		} catch (Exception ex) {
			System.err.println("MyJDBC(getConnection):" + ex.getMessage());
			conn = null;
		}
		return conn;
	}

	/**
	 * 获得prepareStatement
	 * @author dyh 2009-10-24
	 * @param sql
	 * @return prepareStatement
	 */
	public PreparedStatement prepareStatement(String sql) {
		try {
			ps = getConnection().prepareStatement(sql);
		} catch (Exception ex) {
			System.err.println("MyJDBC(prepareStatement):" + ex.getMessage());
			ps = null;
		}

		return ps;
	}

	/**
	 * 获得prepareStatement
	 * @author dyh 2011-4-12
	 * @param sql
	 * @return prepareStatement
	 */
	public PreparedStatement prepareStatement(StringBuilder sql) {
		return prepareStatement(sql.toString());
	}

	/**
	 * 调用存储过程
	 * @author dyh 2009-10-24
	 * @param sql
	 * @return CallableStatement
	 */
	public CallableStatement prepareCall(String sql) {
		try {
			cstmt = getConnection().prepareCall(sql);
		} catch (Exception ex) {
			System.err.println("MyJDBC(prepareCall):" + ex.getMessage());
			cstmt = null;
		}
		return cstmt;
	}

	/**
	 * 设置是否自动COMMIT
	 * @author dyh 2009-10-24
	 * @param flag 是否自动Commit
	 */
	private void setAutoCommit(boolean flag) {
		try {
			bAutoCommit = flag;
			getConnection().setAutoCommit(flag);
		} catch (SQLException ex) {
			System.err.println("MyJDBC(setAutoCommit):" + ex.getMessage());
		}
	}

	/**
	 * 开始事务(将autocommit设置为false)
	 * @author dyh 2009-10-24
	 */
	public void beginTransaction() {
		setAutoCommit(false);
	}

	/**
	 * 提交事务
	 * @author dyh 2009-10-24
	 */
	public void commitTransaction() {
		try {
			if (!bAutoCommit) {
				getConnection().commit();
			}
		} catch (SQLException ex) {
			System.err.println("MyJDBC(commitTransaction):" + ex.getMessage());
		}
	}

	/**
	 * 回滚事务
	 * @author dyh 2009-10-24
	 */
	public void rollbackTransaction() {
		try {
			if (!bAutoCommit) {
				getConnection().rollback();
			}
		} catch (SQLException ex) {
			System.err.println("MyJDBC(rollbackTransaction):" + ex.getMessage());
		}
	}

	/**
	 * 关闭数据库连接
	 * @author dyh 2009-10-24
	 */
	public void close() {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (cstmt != null) {
				cstmt.close();
				cstmt = null;
			}
			if (ps != null) {
				ps.close();
				ps = null;
			}
			if (JDBCUtil.isConnected(conn)) {
				conn.close();
			}
		} catch (Exception ex) {
			System.err.println("MyJDBC(close):" + ex.getMessage());
		} finally {
			conn = null;
			bClosed = true;
		}
	}

	/**
	 * 没有调用 close() 时，执行 close()
	 */
	protected void finalize() {
		try {
			if (!bClosed) {
				close();
			}
		} catch (Exception ex) {
			System.err.println("MyJDBC(finalize):" + ex.getMessage());
		}
	}

	/**
	 * 关闭JDBC
	 * @author dyh 2009-10-24
	 */
	public static void close(MyJDBC myJDBC) {
		try {
			if (myJDBC != null) {
				myJDBC.close();
			}
		} catch (Exception ex) {
			System.err.println("MyJDBC.close(myJDBC):" + ex.getMessage());
		} finally {
			myJDBC = null;
		}
	}
}